package com.one.rope.mvp.web.common.util;

import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.basecore.exception.BaseException;
import com.one.rope.mvp.basecore.exception.NullPointerBusException;

/**
 * 对象判断基本类
 */
public class ObjectUtils {

  /**
   * 必须符合条件
   *
   * @param condition 符合条件true正常,false抛出异常
   * @param message 错误信息
   */
  public static void requsetCondition(boolean condition, String message) {
    if (!condition) {
      throw new BaseException(message);
    }
  }

  public static Object requestNonNull(Object obj) {
    if (obj == null) {
      throw new NullPointerBusException("参数不能为NULL");
    }
    return obj;
  }

  public static Object requestNonNull(Object obj, String message) {
    if (obj == null) {
      throw new NullPointerBusException(message);
    }
    return obj;
  }

  public static Object requestNonEmpty(String obj, String message) {
    if (StringUtils.isNullOrEmpty(obj)) {
      throw new BaseException(message);
    }
    return obj;
  }

  public static Object requestLength(String obj, String message, int maxLength) {
    int length = obj == null ? 0 : obj.length();
    if (length > maxLength) {
      throw new BaseException(message);
    }
    return obj;
  }

  public static Object requestLength(String obj, String minMessage, String maxMessage,
      int minLength, int maxLength) {
    int length = obj == null ? 0 : obj.length();
    if (length < minLength) {
      throw new BaseException(minMessage);
    }

    if (length > maxLength) {
      throw new BaseException(maxMessage);
    }
    return obj;
  }

  public static Object requestNonTrimEmpty(String obj, String message) {
    obj = StringUtils.trim(obj);
    if (StringUtils.isNullOrEmpty(obj)) {
      throw new BaseException(message);
    }
    return obj;
  }

  public static void requsetCondition(boolean condition, String msg, Object... args) {
    if (!condition) {
      throw new BaseException(format(msg, args));
    }
  }

  private static String format(String format, Object... args) {
    if (format == null) {
      return null;
    }
    return args != null ? String.format(format, args) : format;
  }

}
